package com.welyo.view {
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.external.ExternalInterface;
	import flash.net.navigateToURL;
	import flash.net.URLRequest;
	/**
	 * ...
	 * @author vu.doan
	 */
	public class Menu extends Sprite {
		public var mcMenu				:MovieClip;
		
		private var colorActive			:uint = 0xff7200;
		
		private var arrItems			:Array;
		private var arrURL				:Array;
		private var currentIndex		:int;
		
		public function Menu() {
			arrItems = [];
			
			initButton(mcMenu.mcMenu1);
			initButton(mcMenu.mcMenu2);
			initButton(mcMenu.mcMenu3);
			initButton(mcMenu.mcMenu4);
			
			currentIndex = -1;
			
			var index:int = 2;
			if (loaderInfo.parameters.menuIndex) {
				index = parseInt(loaderInfo.parameters.menuIndex);
			}
			
			if (loaderInfo.parameters.url) {
				arrURL = String(loaderInfo.parameters.url).split(",");
			}
			
			activeMenu(index);
		}
		
		public function setURL(arr:Array):void {
			arrURL = arr;
		}
		
		public function activeMenu(index:int):void {
			if (arrItems[currentIndex]) {
				//arrItems[currentIndex].mouseEnabled = arrItems[currentIndex].mouseChildren = false;
				(arrItems[currentIndex] as MovieClip).gotoAndPlay("out");
			}
			
			currentIndex = index;
			if (arrItems[currentIndex]) {
				//arrItems[currentIndex].mouseEnabled = arrItems[currentIndex].mouseChildren = true;
				(arrItems[currentIndex] as MovieClip).gotoAndPlay("over");
			}
		}
		
		private function initButton(item:MovieClip):void {
			arrItems.push(item);
			
			item.mcIcon.mouseEnabled = item.mcIcon.mouseChildren = false;
			item.buttonMode = true;
			item.mcHit.addEventListener(MouseEvent.MOUSE_OVER, onMouseOverButtonHandler);
			item.mcHit.addEventListener(MouseEvent.MOUSE_OUT, onMouseOutButtonHandler);
			item.mcHit.addEventListener(MouseEvent.CLICK, onMouseClickButtonHandler);
		}
		
		private function onMouseOverButtonHandler(evt:MouseEvent):void {
			if (arrItems.indexOf(evt.currentTarget.parent) == currentIndex) {
				return;
			}
			
			evt.currentTarget.parent.gotoAndPlay("over");
		}
		
		private function onMouseOutButtonHandler(evt:MouseEvent):void {
			if (arrItems.indexOf(evt.currentTarget.parent) == currentIndex) {
				return;
			}
			
			evt.currentTarget.parent.gotoAndPlay("out");
		}
		
		private function onMouseClickButtonHandler(evt:MouseEvent):void {
			var index:int = arrItems.indexOf(evt.currentTarget.parent);
			activeMenu(index);
			
			if (arrURL) {
				if (arrURL[index]) {
					navigateToURL(new URLRequest(arrURL[index]), "_self");
				}
			}
		}
	}
}